#include<bits/stdc++.h>
using namespace std;
int t,n,ans=-1;

struct RorB{
    int num,s;
    char col;
}a[200005];

bool cmp(RorB x,RorB y){
    if(x.num==y.num)return x.s>y.s;
    return x.num>y.num;
}

void dfs(int de,int sum){
    if(de>=n){
        ans=max(ans,sum);
        return;
    }
    if(a[de].num==a[de+1].num){
        if(a[de].col==' '&&a[de+1].col==' '){
            a[de].col=a[de+1].col='R';
            for(int i=a[de+1].s+1;i<a[de].s;i++){
                a[i].col='B';
            }
            dfs(de+2,sum+a[de].num);
            for(int i=a[de+1].s;i<=a[de].s;i++){
                a[i].col=' ';
            }
        }else if(a[de].col==a[de+1].col){
            bool flag=true;
            for(int i=a[de+1].s+1;i<a[de].s;i++){
                if(a[i].col==a[de].col){
                    flag=false;
                    break;
                }
            }
            if(flag){
                dfs(de+2,sum+a[de].num);
            }
        }
    }
    dfs(de+1,sum);
    return;
}

int main(){
    //freopen("color.in","r",stdin);
    //freopen("color.out","w",stdout);
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            scanf("%dll",a[i].num);
            a[i].s=i;
            a[i].col=' ';
        }
        sort(a+1,a+n+1,cmp);
        dfs(1,0);
        cout<<ans<<endl;
        ans=-1;
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
